type StorageSyncCache = {
  set: (key: string, value: string) => void
  get: (key: string) => string | null
  setJSON: (key: string, value: object | any[]) => void
  getJSON: (key: string) => object | any[] | null
  remove: (key: string) => void
  clear: () => void
}

export const storageSyncCache: StorageSyncCache = {
  set(key: string, value: string) {
    if (key != undefined && value != undefined) {
      uni.setStorageSync(key, value)
    }
  },
  get(key: string) {
    if (key == undefined) {
      return null
    }
    return uni.getStorageSync(key)
  },
  setJSON(key: string, value: object | any[]) {
    if (key != undefined && value != undefined) {
      uni.setStorageSync(key, JSON.stringify(value))
    }
  },
  getJSON(key: string) {
    const value = this.get(key)
    if (value != undefined) {
      return value ? JSON.parse(value) : null
    } else {
      return null
    }
  },
  remove(key: string) {
    if (key != undefined) {
      uni.removeStorageSync(key)
    }
  },
  clear() {
    uni.clearStorageSync()
  },
}
